package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.RankRule;
import com.novax.ex.activity.infrastructure.entity.RankSubRule;
import com.novax.ex.activity.open.api.inner.RankRuleInnerApi;
import com.novax.ex.activity.open.model.request.RankRuleRequest;
import com.novax.ex.activity.open.model.request.RankSubRuleRequest;
import com.novax.ex.activity.open.model.response.RankRuleResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.RankRuleService;
import com.novax.ex.activity.provider.service.RankSubRuleService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
public class RankRuleInnerController extends BaseActivityController implements RankRuleInnerApi {

    @Resource
    private RankRuleService rankRuleService;
    @Resource
    private RankSubRuleService rankSubRuleService;


    @Override
    public ReturnResult<List<RankRuleResponse>> ruleList(Long rankId) {
        return ReturnResult.success(CopyUtils.copyList(rankRuleService.ruleList(rankId), RankRuleResponse.class));
    }

    @Override
    public ReturnResult<?> add(String language, RankRuleRequest ruleRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }

        // 查询是有已有配置多语言
        RankRule rankRule = rankRuleService.findByRankIdAndLanguage(ruleRequest.getRankId(), ruleRequest.getLanguage());
        if(Objects.nonNull(rankRule)){
            return ReturnResult.fail(getInternational(language, InternationalCode.ADMIN_IDENTICAL_DATA));
        }

        RankRule request = CopyUtils.copyObject(ruleRequest, RankRule.class);
        boolean flag = rankRuleService.addSelective(request);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS)) :
            ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> modify(String language, Long id, RankRuleRequest ruleRequest) {
        RankRule rankRule = CopyUtils.copyObject(ruleRequest, RankRule.class);
        rankRule.setId(id);
        boolean flag = rankRuleService.modifySelective(rankRule);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS)) :
            ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<List<RankRuleResponse>> subRuleList(Long rankSubId) {
        return ReturnResult.success(CopyUtils.copyList(rankSubRuleService.subRuleList(rankSubId), RankRuleResponse.class));
    }

    @Override
    public ReturnResult<?> subAdd(String language, RankSubRuleRequest ruleRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }

        // 查询是有已有配置多语言
        RankSubRule rankSubRule =  rankSubRuleService.findByRankIdAndLanguage(ruleRequest.getRankSubId(), ruleRequest.getLanguage());
        if(Objects.nonNull(rankSubRule)){
            return ReturnResult.fail(getInternational(language, InternationalCode.ADMIN_IDENTICAL_DATA));
        }

        RankSubRule request = CopyUtils.copyObject(ruleRequest, RankSubRule.class);
        boolean flag = rankSubRuleService.addSelective(request);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS)) :
            ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> subModify(String language, Long id, RankSubRuleRequest ruleRequest) {
        RankSubRule rankSubRule = CopyUtils.copyObject(ruleRequest, RankSubRule.class);
        rankSubRule.setId(id);
        boolean flag = rankSubRuleService.modifySelective(rankSubRule);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS)) :
            ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

}
